package com.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.SessionData;
import com.server.service.IMngMenuService;
import com.server.service.IMngRoleService;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.SessionUtil;

@Controller("MngRoleController")
@RequestMapping("adminRole")
public class MngRoleController {
	@Autowired
	private IMngRoleService mngRoleService;
	@Autowired
	private IMngMenuService mngMenuService;
	
	@RequestMapping("")
	public ModelAndView roleList(HttpServletRequest request){
		List<Map<String, Object>> list = mngRoleService.roleList();
		ModelAndView mv = new ModelAndView("mngRole/roleList");
		mv.addObject("list", list);
		return mv;
	}
	@RequestMapping("addRole")
	public ModelAndView addRole(HttpServletRequest request,HttpServletResponse response){
		if(request.getMethod().equalsIgnoreCase("post")){
			Map<String, Object> inputMap = new HashMap<String, Object>();
			Map<String, Object> map = new HashMap<String, Object>();
			String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
			String name = request.getParameter("name");
			String describe = request.getParameter("describe");
			String authority = request.getParameter("authority");
			inputMap.put("name", name);
			inputMap.put("description", describe);
			inputMap.put("authority", authority);
			Boolean isSuccess = mngRoleService.addRole(inputMap, operator);
			if(isSuccess){
				map.put("isSuccess", isSuccess);
				map.put("info", "添加成功");
			}else {
				map.put("isSuccess", isSuccess);
				map.put("info", "添加失败");
			}
			OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
			return null;
		}
		List<Map<String, Object>> menuList = mngMenuService.menuList();
		ModelAndView mv = new ModelAndView("mngRole/roleAdd");
		mv.addObject("menuList", menuList);
		return mv;
	}
	@RequestMapping("updateRole")
	public ModelAndView updateRole(HttpServletRequest request,HttpServletResponse response){
		if(request.getMethod().equalsIgnoreCase("post")){
			Map<String, Object> inputMap = new HashMap<String, Object>();
			Map<String, Object> map = new HashMap<String, Object>();
			String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
			String name = request.getParameter("name");
			String describe = request.getParameter("describe");
			String authority = request.getParameter("authority");
			inputMap.put("name", name);
			inputMap.put("description", describe);
			inputMap.put("authority", authority);
			String id = request.getParameter("id");
			Boolean isSuccess = mngRoleService.updateRole(inputMap, "id", id, operator);
			if(isSuccess){
				map.put("isSuccess", isSuccess);
				map.put("info", "修改成功");
			}else {
				map.put("isSuccess", isSuccess);
				map.put("info", "修改失败");
			}
			OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
			return null;
		}
		int id = Integer.parseInt(request.getParameter("id"));
		Map<String, Object> roleMap = mngRoleService.getRoleById(id);
		String auth = (String) roleMap.get("authority");
		/*String arr[] = new String[20];
		if(!StringUtils.isEmpty(auth)){
			 arr = auth.split(",");
		}*/
		List<Map<String, Object>> menuList = mngMenuService.menuList();
		ModelAndView mv = new ModelAndView("mngRole/roleUpdate");
		mv.addObject("roleMap", roleMap);
		mv.addObject("role", auth);
		mv.addObject("menuList", menuList);
		return mv;
	}
	@RequestMapping("deleteRole")
	public ModelAndView deleteRole(HttpServletRequest request){
		String id = request.getParameter("id");
		Boolean isSuccess = mngRoleService.deletRole("id", id);
		if(isSuccess){
			return new ModelAndView("redirect:/adminRole");
		}
		return null;
	}
}
